// @FileName  :adjust.c
// @Time      :2024/4/25 18:52
// @Author    :YKW
//# include <bits/stdc++.h>
//using namespace std;
#include<stdio.h>
#include<stdlib.h>

struct intersection {
    int s[3];
} in[1024];
struct leaf {
    int id, flow;
} lf[1024];
int lfcnt, ascnt, num, root, ans[1024], bin;

int cmp(const void *a, const void *b) {
    struct leaf *la = (struct leaf *) a, *lb = (struct leaf *) b;
    if (la->flow == lb->flow) return la->id - lb->id;//升序
    return lb->flow - la->flow;//降序
}

int main() {
//    char line[1024];
//    while(gets(line)!=NULL){
//        printf("%s",line);
//    }
    setbuf(stdout, NULL);
    while ((scanf("%d", &num)) && num != -1) {
        if (root == 0)root = num;
        for (int i = 0; i < 2; i++)
            scanf("%d", &in[num].s[i]);
        if (in[num].s[1] != -1)scanf("%d", &in[num].s[2]);
        if (in[num].s[1] != -1 & in[num].s[2] != -1)scanf("%d", &bin);
    }
    while ((scanf("%d%d", &lf[lfcnt].id, &lf[lfcnt].flow)) == 2)
        lfcnt++;
    qsort(lf, lfcnt, sizeof(struct leaf), cmp);
    int queue[1024], st = 0, ed = 0;
    queue[ed++] = root;
    while (st < ed) {
        int cur = queue[st++];
        if (cur > 0 && cur < 100) ans[ascnt++] = cur;
        else
            for (int i = 0; i < 3; i++)
                if (in[cur].s[i] > 0)
                    queue[ed++] = in[cur].s[i];
    }
    for (int i = 0; i < ascnt; i++) {
        printf("%d->%d\n", lf[i].id, ans[i]);
    }
}
/*
100 101 102 103 -1
103 3 4 5 -1
102 104 -1
101 6 7 8 -1
104 1 2 -1
-1
5 1001
2 895
4 2850
1 3250
8 1860
3 795
7 1200
6 985
 */